package com.manage.controller; /**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/27 13:35
 */

import com.manage.dao.UserDaoImpl;
import com.manage.service.UserService;
import com.manage.service.UserServiceImpl;
import com.manage.util.MD5Utils;
import com.manage.util.StrUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.接收前端传来的数据
        String username = request.getParameter("username");
        String pwd = request.getParameter("password");
        //对密码进行加密
        String password = MD5Utils.md5(pwd);

        //2.判断数据是否为空
        if (!StrUtils.empty(username) || !StrUtils.empty(password)){
            //为空,跳转到登录页
            response.sendRedirect(request.getContextPath()+"/login.html");
            return;
        }
        //3.调用service,判断用户名 密码是否正确
        UserService userService = new UserServiceImpl(new UserDaoImpl());
        userService.register(username,password);
        //4.重定向到登录页
        response.sendRedirect(request.getContextPath()+"/login.html");
    }
}
